package com.burstly.lib.component;

import com.burstly.lib.network.beans.RequestData;
import com.burstly.lib.network.beans.ResponseBean;
import com.burstly.lib.network.beans.ResponseBean.ResponseData;
import com.burstly.lib.persistance.ResponseSaver;
import com.burstly.lib.ui.BurstlyView;
import com.burstly.lib.util.LoggerExt;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import junit.framework.Assert;

public final class ComponentQueue
{
  public static final String ADVERTISE = "advertise";
  public static final String CONTEXT = "context";
  private static final LoggerExt LOG = LoggerExt.getInstance();
  public static final String RESPONSE_SAVER = "responseSaver";
  private static final String TAG = "ComponentQueue";
  private IInternalBurstlyAdListener mBurstlyAdListener;
  final BurstlyView mBurstlyView;
  private Map<String, String> mClientTargetingParams;
  IQueueComponentListener mComponentQueueListener;
  IAdaptorController mCurrentComponentController;
  ComponentQueueElement mCurrentElement;
  private Map<String, Map<String, ?>> mCustomNetworkParams;
  Set<FailedToShow> mFailedToShow;
  final RequestData mFullRequestData;
  private Boolean mIsInterstitial;
  private boolean mIsStopped;
  private final IAdaptorController.IComponentQueueLifecycleListener mLcListener = new IAdaptorController.IComponentQueueLifecycleListener()
  {
    private void handleFailedItems(List<FailedToShow> paramAnonymousList)
    {
      if (ComponentQueue.this.mFailedToShow == null)
        throw new IllegalArgumentException("Can not precess queue without parent failed to show collector!");
      if (!paramAnonymousList.isEmpty())
        ComponentQueue.this.mFailedToShow.addAll(paramAnonymousList);
    }

    public void failedToShow(List<FailedToShow> paramAnonymousList)
    {
      handleFailedItems(paramAnonymousList);
      ComponentQueue.LOG.logInfo("ComponentQueue", "Component was failed to show! Moving to next one...", new Object[0]);
      ComponentQueue.this.printFailedToShowComponents();
      ComponentQueue.this.execute();
    }

    public void startAutorefreshByParam(int paramAnonymousInt)
    {
      ComponentQueue.this.mComponentQueueListener.scheduleRequest(paramAnonymousInt);
    }

    public void successToshow(List<FailedToShow> paramAnonymousList)
    {
      handleFailedItems(paramAnonymousList);
      ComponentQueue.this.printUnshownComponents();
      ComponentQueue.this.printFailedToShowComponents();
      ComponentQueue.LOG.logInfo("ComponentQueue", "Component showing was tracked on server, stopping queue execution...", new Object[0]);
    }
  };
  final LinkedList<ComponentQueueElement> mQueueElements;
  ResponseSaver<ResponseBean> mResponseSaver;

  public ComponentQueue(Collection<ComponentQueueElement> paramCollection, RequestData paramRequestData, BurstlyView paramBurstlyView, ResponseSaver<ResponseBean> paramResponseSaver)
  {
    Assert.assertNotNull("Queue components can not be null!", paramCollection);
    Assert.assertNotNull("Queue fullRequestData can not be null!", paramRequestData);
    Assert.assertNotNull("Parent can not be null!", paramBurstlyView);
    this.mBurstlyView = paramBurstlyView;
    this.mFullRequestData = paramRequestData;
    this.mQueueElements = new LinkedList(paramCollection);
    this.mResponseSaver = paramResponseSaver;
  }

  private void configureCurrentComponent()
  {
    this.mCurrentComponentController.setParent(this.mBurstlyView);
    this.mCurrentComponentController.setLifeCycleListener(this.mLcListener);
    this.mCurrentComponentController.setBursltlyAdListener(this.mBurstlyAdListener);
  }

  private Map<String, Object> createControllerParameters()
  {
    HashMap localHashMap = new HashMap(5);
    localHashMap.put("context", this.mBurstlyView.getContext());
    localHashMap.put("advertise", this.mCurrentElement.getCreativeData());
    localHashMap.put("responseSaver", this.mResponseSaver);
    return localHashMap;
  }

  private IAdaptorController getController()
  {
    IAdaptorController localIAdaptorController = null;
    while (true)
    {
      ComponentQueueElement localComponentQueueElement = (ComponentQueueElement)this.mQueueElements.poll();
      this.mCurrentElement = localComponentQueueElement;
      if (localComponentQueueElement == null)
        break;
      Map localMap = createControllerParameters();
      localIAdaptorController = this.mCurrentElement.getController(localMap);
      if (localIAdaptorController != null)
      {
        String str = localIAdaptorController.getNetworkName();
        localIAdaptorController.setCustomNetworkParams((Map)this.mCustomNetworkParams.get(str));
        if (this.mIsInterstitial != null)
          localIAdaptorController.setInterstitialMode(this.mIsInterstitial.booleanValue());
        localIAdaptorController.setClientTargetParams(this.mClientTargetingParams);
        return localIAdaptorController;
      }
      this.mFailedToShow.add(new FailedToShow(this.mFullRequestData.getPublisher(), this.mFullRequestData.getZone(), this.mCurrentElement.getCreativeData().getCrid()));
      LoggerExt localLoggerExt = LOG;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = this.mCurrentElement;
      localLoggerExt.logInfo("ComponentQueue", "{0} has been added to failed collector.", arrayOfObject);
    }
    return localIAdaptorController;
  }

  private void notifyComponentChange()
  {
    if (this.mComponentQueueListener != null)
      this.mComponentQueueListener.componentChanged(this.mCurrentComponentController);
  }

  public void execute()
  {
    LOG.logInfo("ComponentQueue", "Executing main queue...", new Object[0]);
    if (!this.mIsStopped)
    {
      this.mCurrentComponentController = getController();
      if (this.mCurrentComponentController != null)
      {
        configureCurrentComponent();
        notifyComponentChange();
        this.mCurrentComponentController.start();
        return;
      }
      LOG.logInfo("ComponentQueue", "No more components in queue.", new Object[0]);
      this.mComponentQueueListener.rerequestServer();
      return;
    }
    LOG.logInfo("ComponentQueue", "Queue was stopped.", new Object[0]);
  }

  IAdaptorController getCurrentComponent()
  {
    return this.mCurrentComponentController;
  }

  public Set<FailedToShow> getFaildToShowCollector()
  {
    return this.mFailedToShow;
  }

  void printFailedToShowComponents()
  {
    if (!this.mFailedToShow.isEmpty())
    {
      StringBuilder localStringBuilder = new StringBuilder();
      Iterator localIterator = this.mFailedToShow.iterator();
      while (localIterator.hasNext())
        localStringBuilder.append(((FailedToShow)localIterator.next()).toString()).append("\n");
      LoggerExt localLoggerExt = LOG;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = localStringBuilder.toString();
      localLoggerExt.logInfo("ComponentQueue", "Failed to show components are: {0}", arrayOfObject);
    }
  }

  void printUnshownComponents()
  {
    if (!this.mQueueElements.isEmpty())
    {
      StringBuilder localStringBuilder = new StringBuilder();
      Iterator localIterator = this.mQueueElements.iterator();
      while (localIterator.hasNext())
        localStringBuilder.append(((ComponentQueueElement)localIterator.next()).toString());
      LOG.logInfo("ComponentQueue", "Components that were not shown because one of them had been already shown successfully: {0}", new Object[] { localStringBuilder });
      return;
    }
    LOG.logInfo("ComponentQueue", "All advertise components were handled.", new Object[0]);
  }

  public void setBurstlyAdListener(IInternalBurstlyAdListener paramIInternalBurstlyAdListener)
  {
    this.mBurstlyAdListener = paramIInternalBurstlyAdListener;
  }

  public void setComponentChangeListener(IQueueComponentListener paramIQueueComponentListener)
  {
    this.mComponentQueueListener = paramIQueueComponentListener;
  }

  public void setCustomNetworkParams(Map<String, Map<String, ?>> paramMap)
  {
    this.mCustomNetworkParams = paramMap;
  }

  public void setExplicitParams(Map<String, String> paramMap)
  {
    this.mClientTargetingParams = paramMap;
  }

  public void setFaildToShowCollector(Set<FailedToShow> paramSet)
  {
    this.mFailedToShow = paramSet;
    LOG.logInfo("ComponentQueue", "New failed to show items collector was set", new Object[0]);
  }

  public void setInterstitialMode(boolean paramBoolean)
  {
    this.mIsInterstitial = Boolean.valueOf(paramBoolean);
  }

  public void stopQueue()
  {
    this.mIsStopped = true;
    this.mQueueElements.clear();
    if (this.mCurrentComponentController != null)
      this.mCurrentComponentController.stop();
  }

  public static abstract interface IQueueComponentListener
  {
    public abstract void componentChanged(IAdaptorController paramIAdaptorController);

    public abstract void rerequestServer();

    public abstract void scheduleRequest(int paramInt);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.component.ComponentQueue
 * JD-Core Version:    0.6.2
 */